import typing

from _hellchin_webrun.src.core.browser.base import BaseTest
# from _hellchin_webrun.src.core.utils.logger.run_logger import TestLogger
from _hellchin_webrun.src.core.utils.chinese_alias import KeyWordManage


class ElementMixin(BaseTest):
    """页面元素相关的操作"""

    @KeyWordManage.chinese_alias("输入内容", "填写值")
    def fill_value(self, locator, value, timeout=None):
        """
        在输入框中填入值

        :param locator: 元素定位器
        :param value: 要填入的值
        :param timeout: 超时时间
        :return:
        """

        self.log.info("填写值", f"{locator} = {value}")
        # self.page.fill(locator, value, timeout=timeout)
        self.page.locator(locator).fill(value, timeout=timeout)  # 推荐使用这个，符合 Playwright 的推荐使用方式。
        self.log.info("填写值", f"{locator} = {value}")

    @KeyWordManage.chinese_alias("点击元素", "点击")
    def click(self, locator, *, timeout: typing.Optional[float] = None):
        """
        点击元素
        :param locator: 元素定位器
        :param timeout: 超时时间
        :return:
        """

        self.log.info("点击元素", locator)
        # self.page.click(locator)
        self.page.locator(locator).click(timeout=timeout)

        self.log.info("点击元素", locator)

    @KeyWordManage.chinese_alias("鼠标悬停到元素上方", "悬停到元素", "悬停")
    def hover(self, locator, timeout=3000):
        """悬停到元素上方"""

        # print(f"正在悬停到元素:{locator}")
        self.log.info("悬停到元素", locator)
        self.page.locator(locator).hover(timeout=timeout)
        self.log.info("悬停到元素", locator)

    @KeyWordManage.chinese_alias("选择下拉框的选项", "选择下拉框选项", "选择下拉框")
    def select_option(self, locator, value, timeout=3000):
        """选择下拉框选项"""

        # print(f"正在选择下拉框:{locator}，选项的值:{value}")
        self.log.info("选择下拉框", f"{locator} = {value}")
        self.page.locator(locator).select_option(value, timeout=timeout)
        self.log.info("选择下拉框", f"{locator} = {value}")

    @KeyWordManage.chinese_alias("拖拽元素", "拖拽")
    def drag_and_drop(self, start_selector, end_selector, timeout=3000):
        """拖拽元素"""
        # print(f"正在拖拽元素:{start_selector}，拖拽到的元素:{end_selector}")
        self.log.info("拖拽元素", f"{start_selector} -> {end_selector}")
        self.page.locator(start_selector).drag_to(self.page.locator(end_selector))
        self.log.info("拖拽元素", f"{start_selector} -> {end_selector}")
